package LeetCode刷题;

/**
 * @program: Java_Study
 * @author: Xiaofan
 * @createTime: 2022-01-25 09:23
 * @description: Functions of this class is
 * 民间高手解法：
 * class Solution {
 *     public int[] exchange(int[] nums) {
 *         int i = 0, j = nums.length - 1, tmp;
 *         while(i < j) {
 *             while(i < j && (nums[i] & 1) == 1) i++;
 *             while(i < j && (nums[j] & 1) == 0) j--;
 *             tmp = nums[i];
 *             nums[i] = nums[j];
 *             nums[j] = tmp;
 *         }
 *         return nums;
 *     }
 * }
 **/
public class 调整数组顺序使奇数位于偶数前面 {
    //我的解法，慢个1ms
    public int[] exchange(int[] nums) {
        int len=nums.length;
        int index=0;
        for(int i=0;i<len;i++){
            if(nums[i]%2==1){
                swap(nums,index++,i);
            }

        }
        return nums;
    }
    public void swap(int nums[],int i,int j){
        int temp=nums[i];
        nums[i]=nums[j];
        nums[j]=temp;
    }
}